package news.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.ArrayList;
import java.util.List;

import base.BaseFragment;
import base.BasePage;
import home.FunctionPage;
import home.GovAffairsPage;
import home.NewsCenterPage;
import home.SettingPage;
import home.SmartServiecePage;
import httptool.getHttpUrl;
import shanshuitiancheng.news.R;
import view.CustomViewPager;
import view.LazyViewPager;

/**
 * Created by cch on 2016/6/22.
 */
public class HomeFragment1 extends BaseFragment {
    private final String TAG = "我点击了";
    private View view;
    private CustomViewPager viewpager;
    private RadioGroup main_radio;
    private HomepageAdapter1 adapters;
    private int checkedId = R.id.rb_function;

    @Override
    public View initView(LayoutInflater inflater) {
        view = inflater.inflate(R.layout.frag_home2,null);
        viewpager = (CustomViewPager) view.findViewById(R.id.viewpager);
        main_radio = (RadioGroup) view.findViewById(R.id.main_radio);
        return  view;
    }

    List<BasePage> list = new ArrayList<BasePage>();
    @Override
    public void initData(Bundle savedInstanceState) {
        list.add(new FunctionPage(ct, getHttpUrl.FunctionUrl));
        list.add(new NewsCenterPage(ct));
        list.add(new SmartServiecePage(ct));
        list.add(new GovAffairsPage(ct));
        list.add(new SettingPage(ct));

        adapters = new HomepageAdapter1(ct, list);
        viewpager.setAdapter(adapters);
        list.get(0).initData();
        viewpager.setCurrentItem(0);
        viewpager.setOnPageChangeListener(new LazyViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (0 == position) {
                    sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                } else {
                    sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
                }
                BasePage page = list.get(position);
                page.initData();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        main_radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_function:
                        viewpager.setCurrentItem(0, true);
                        checkedId = 0;
                        break;
                    case R.id.rb_news_center:
                        viewpager.setCurrentItem(1, true);
                        checkedId = 1;
                        break;
                    case R.id.rb_smart_service:
                        viewpager.setCurrentItem(2, true);
                        checkedId = 2;
                        break;
                    case R.id.rb_gov_affairs:
                        viewpager.setCurrentItem(3, true);
                        checkedId = 3;
                        break;
                    case R.id.rb_setting:
                        viewpager.setCurrentItem(4, true);
                        checkedId = 4;
                        break;
                }
            }
        });
        main_radio.check(checkedId);
    }



    private class HomepageAdapter1 extends PagerAdapter{
        private Context ct;
        private List<BasePage> list;
        public HomepageAdapter1(Context ct, List<BasePage> list) {
            this.ct = ct;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            super.destroyItem(container, position, object);
//            ((CustomViewPager)container).removeView(list.get(position).getRootView());
            ((CustomViewPager)container).removeView(list.get(position).getRootView());
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ((CustomViewPager)container).addView(list.get(position).getRootView());
            return list.get(position).getRootView();
        }
    }
}
